package com.suitsoft.ut.web.exp;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.opensymphony.xwork2.ActionSupport;
import com.suitsoft.ut.commons.AppConfig;
import com.suitsoft.ut.commons.LogAnnotation;
import com.suitsoft.ut.entity.base.College;
import com.suitsoft.ut.entity.base.Discipline;
import com.suitsoft.ut.service.base.CollegeManager;
import com.suitsoft.ut.service.commons.DisciplineManager;
import com.suitsoft.ut.service.commons.ExpManager;
import com.suitsoft.ut.util.ZipManager;

/**
 * 导出
 *@author Suine
 */
public class ExpAction extends ActionSupport{
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	private ExpManager expManager;
	private DisciplineManager disciplineManager;
	private CollegeManager collegeManager;
	
	private List<Long> ids;
	private List<Map<String,Object>> result;
	private String collge;
	private String zhuanye;
	private String studentNo;
	private String studentName;
	private String teacherName;
	private String teacherNo;
	private Page<Map<String,Object>> page = new Page<Map<String,Object>>(AppConfig.getInstance().getPageSize());
	/** 区分 导出类型 
	 *  1 - 立题
	 *  2 - 任务书
	 *  3 - 开题情况表
	 *  4 - 开题报告
	 *  5 - 周记
	 * */
	private List<Long> flag;
	
	@Override
	public String execute() throws Exception {
		expManager.findByPage(page, collge, zhuanye, studentNo, studentName, teacherName, teacherNo);
		if(page.getTotalCount() == 0){
			addActionMessage("无相关信息");
			return SUCCESS;
		}
		return SUCCESS;
	}
	
	/**
	 *Ajax获取专业
	 * @return
	 */
	public String ajaxZhuanye(){
		try {
			PrintWriter out = Struts2Utils.getResponse().getWriter();
			College college = collegeManager.findByName(collge) == null ? null : collegeManager.findByName(collge).get(0);
			JSONArray array = new JSONArray();
			JSONObject obj = null;
			if(college != null){
				for(Discipline discipline : college.getDiscipline()){
					obj = new JSONObject();
					obj.put("name", discipline.getDisciplineName());
					array.add(obj);
				}
				out.print(array.toString());
				out.flush();
				out.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@LogAnnotation(log= "导出 - 导出")
	public String exp(){
		if(ids == null || ids.isEmpty()){
			addActionMessage("请选择题目，再导出！");
			return SUCCESS;
		}
		if(flag == null || flag.isEmpty()){
			addActionMessage("请选择导出类型！");
			return SUCCESS;
		}
		
		String desPath = expManager.exp(ids, flag, ServletActionContext.getServletContext().getRealPath("/"));
		File file = new File(desPath);
		Struts2Utils.getRequest().setAttribute("exportFilePath", desPath + "/");
		Struts2Utils.getRequest().setAttribute("exportFileName", file.getName());
		Struts2Utils.getRequest().setAttribute("type", "zip");
		try {
			Struts2Utils.getRequest().getRequestDispatcher("/download.jsp").forward(Struts2Utils.getRequest(), Struts2Utils.getResponse());
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	@LogAnnotation(log= "导出 - 导出蓝皮书")
	public String expBlue(){
		if(ids == null || ids.isEmpty()){
			addActionMessage("请选择题目，再导出！");
			return SUCCESS;
		}
		String desPath = "";;
		try {
			desPath = expManager.expLanPi(ids, ServletActionContext.getServletContext().getRealPath("/"));
		} catch(Exception e){
			e.printStackTrace();
			addActionMessage("导出失败，可能是此论文还没有结束");
			return SUCCESS;
		}
		File file = new File(desPath);
		Struts2Utils.getRequest().setAttribute("exportFilePath", desPath + "/");
		Struts2Utils.getRequest().setAttribute("exportFileName", file.getName());
		Struts2Utils.getRequest().setAttribute("type", "zip");
		try {
			Struts2Utils.getRequest().getRequestDispatcher("/download.jsp").forward(Struts2Utils.getRequest(), Struts2Utils.getResponse());
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}

	public List<Long> getIds() {
		return ids;
	}
	public void setIds(List<Long> ids) {
		this.ids = ids;
	}
	public List<Map<String, Object>> getResult() {
		return result;
	}
	public void setResult(List<Map<String, Object>> result) {
		this.result = result;
	}
	public String getCollge() {
		return collge;
	}
	public void setCollge(String collge) {
		this.collge = collge;
	}
	public String getZhuanye() {
		return zhuanye;
	}
	public void setZhuanye(String zhuanye) {
		this.zhuanye = zhuanye;
	}
	public String getStudentNo() {
		return studentNo;
	}
	public void setStudentNo(String studentNo) {
		this.studentNo = studentNo;
	}
	public String getStudentName() {
		return studentName;
	}
	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}
	@Autowired
	public void setExpManager(ExpManager expManager) {
		this.expManager = expManager;
	}

	public String getTeacherName() {
		return teacherName;
	}

	public void setTeacherName(String teacherName) {
		this.teacherName = teacherName;
	}

	public String getTeacherNo() {
		return teacherNo;
	}

	public void setTeacherNo(String teacherNo) {
		this.teacherNo = teacherNo;
	}

	public Page<Map<String, Object>> getPage() {
		return page;
	}

	public void setPage(Page<Map<String, Object>> page) {
		this.page = page;
	}
	@Autowired
	public void setDisciplineManager(DisciplineManager disciplineManager) {
		this.disciplineManager = disciplineManager;
	}
	@Autowired
	public void setCollegeManager(CollegeManager collegeManager) {
		this.collegeManager = collegeManager;
	}
	public List<Long> getFlag() {
		return flag;
	}
	public void setFlag(List<Long> flag) {
		this.flag = flag;
	}
}

